#!/usr/bin/env python3
# -*- coding:utf-8 -*-

# https://segmentfault.com/a/1190000009409635
# python实现屏保计时器

# 实现Python程序exe软件化:
# pyinstaller -F dpython.py  //可以通过-F 参数对 Python 源文件生成一个独立的可执行文件

import turtle
import time


def drawGap(printer):
    printer.penup()
    printer.fd(5)


def drawLine(printer, draw):
    drawGap(printer)
    printer.pendown() if draw else printer.penup()
    printer.fd(40)
    drawGap(printer)
    printer.right(90)


def drawDigit(printer, d):
    drawLine(printer, True) if d in [2, 3, 4, 5, 6, 8, 9] else drawLine(printer, False)  # g
    drawLine(printer, True) if d in [0, 1, 3, 4, 5, 6, 7, 8, 9] else drawLine(printer, False)  # c
    drawLine(printer, True) if d in [0, 2, 3, 5, 6, 8, 9] else drawLine(printer, False)  # d
    drawLine(printer, True) if d in [0, 2, 6, 8] else drawLine(printer, False)  # e
    printer.left(90)  # 经历一次右转后，调整左转，方向竖直向上
    drawLine(printer, True) if d in [0, 4, 5, 6, 8, 9] else drawLine(printer, False)
    drawLine(printer, True) if d in [0, 2, 3, 5, 6, 7, 8, 9] else drawLine(printer, False)
    drawLine(printer, True) if d in [0, 1, 2, 3, 4, 7, 8, 9] else drawLine(printer, False)
    printer.left(180)
    printer.penup()
    printer.fd(20)


def drawDate(printer, date):
    printer.pencolor('red')
    for i in date:
        if i == '-':
            printer.write('时', font=('Arial', 18, 'normal'))
            printer.pencolor('green')
            printer.fd(40)
        elif i == '=':
            printer.write('分', font=('Arial', 18, 'normal'))
            printer.pencolor('blue')
            printer.fd(40)
        elif i == '+':
            printer.write('秒', font=('Arial', 18, 'normal'))
            printer.pencolor('yellow')
        else:
            drawDigit(printer, eval(i))


def init(window, printer):
    window.setup(1920, 1080, 0, 0)  # 设置画布大小 200 200 为屏幕位置
    printer.speed(10)
    printer.penup()
    printer.goto(0, 0)
    printer.fd(-350)
    printer.pensize(5)


def main():
    window = turtle.Screen()
    printer = turtle.Turtle()
    while True:
        printer.clear()
        init(window, printer)
        time_string = time.strftime("%H-%M=%S+", time.localtime())
        printer.getscreen().tracer(30, 0)
        drawDate(printer, time_string)  # 格式化时间 2017-05=02+ 控制输入年日月
        time.sleep(1)
        printer.hideturtle()


if __name__ == "__main__":
    main()
